package com.way.gesture;

import java.io.File;
import java.util.List;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.way.util.AppUtil;
import com.way.util.ApplicationInfo;
import com.way.util.GestureLibraryManager;

public class SelectAppActivity extends ListActivity {
	private Gesture mGesture;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_list);
		AppUtil.transWindows(this,
				getResources().getColor(R.color.material_teal));
		getActionBar().setDisplayHomeAsUpEnabled(true);
		List<ApplicationInfo> apps = AppUtil.getAllApps(this);
		AppAdapter adapter = new AppAdapter(this, apps);
		setListAdapter(adapter);
		mGesture = getIntent().getParcelableExtra("gesture");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		ApplicationInfo info = (ApplicationInfo) l.getAdapter().getItem(
				position);
		ComponentName componentName = info.componentName;
		StringBuilder stringBuilder = new StringBuilder(
				String.valueOf(SelectTaskActivity.APP_COMMAND));
		stringBuilder.append(",");
		stringBuilder.append(componentName.getPackageName());
		stringBuilder.append(",");
		stringBuilder.append(componentName.getClassName());
		stringBuilder.append(",");
		stringBuilder.append(info.title);
		Log.i("liweiping", stringBuilder.toString());

		GestureLibraryManager gestureLibraryManager = GestureLibraryManager
				.getInstance(this);
		if (TextUtils.equals(getIntent().getAction(), "com.way.edit")) {
			String name = getIntent().getStringExtra("task");
			if (mGesture == null) {
				Gesture gesture = gestureLibraryManager.getGesture(name);
				gestureLibraryManager.removeGesture(name);
				gestureLibraryManager.updateGesture(stringBuilder.toString(),
						gesture);
				finish();
				return;
			}
		}
		if (mGesture != null) {
			gestureLibraryManager.updateGesture(stringBuilder.toString(),
					mGesture);
			finish();
			return;
		}
		
		Intent i = new Intent(this, CreateGestureActivity.class);
		i.putExtra("task", stringBuilder.toString());
		startActivity(i);
		finish();
	}

	private class AppAdapter extends ArrayAdapter<ApplicationInfo> {
		private final LayoutInflater mInflater;

		public AppAdapter(Context context, List<ApplicationInfo> objects) {
			super(context, 0, objects);
			mInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.gestures_item, parent,
						false);
			}
			final TextView label = (TextView) convertView;
			ApplicationInfo info = getItem(position);
			label.setTag(info);
			label.setText(info.title);
			label.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(
					getContext().getResources(), info.iconBitmap), null, null,
					null);

			return convertView;
		}

	}
}
